<?php
declare(strict_types=1);

namespace App\Service\FaceService;

use App\Service\Code\Normal;
use App\Service\BaseService;
use App\Service\VendorClient\FacePlus\FacePlusApi;
use App\Model\FaceServiceModel\Face;
use App\Model\FaceServiceModel\User;

class FaceService extends BaseService
{
    /**
     * 识别身份证
     * @param array $param
     * @return type
     */
    public function ocrIdCard(array $param) {
        if(empty($param['image_base64'])){
            return Normal::returnCode(Normal::FAIL,'图片的base64加密的二进制数据必传');
        }
        $response=(new FacePlusApi())->ocrIdCard($param);        
        if($response['code']!= Normal::SUC){
            return $response;
        }
        if(empty($response['data']['cards'])){
            return Normal::returnCode(Normal::FAIL, '未识别到身份证');
        }
        $card=$response['data']['cards'][0];
        if($card['side']=='back'){
            return Normal::returnCode(Normal::FAIL, '非身份证正面');
        }
        $data=[
            'name'=>$card['name'],
            'address'=>$card['address'],
            'idcard'=>$card['id_card_number'],
            'gender'=>$card['gender'],
            'birthday'=>$card['birthday'],
            'race'=>$card['race']
        ];
        return   Normal::returnCode(Normal::SUC, '识别成功',$data);
    }
    
    /**
     * 人脸检测
     * @param array $param
     * @return type
     */
    public function faceCheck(array $param) {
        if(empty($param['image_base64'])){
            return Normal::returnCode(Normal::FAIL,'图片的base64加密的二进制数据必传');
        }
        $response=(new FacePlusApi())->ocrIdCard($param);        
        if($response['code']!= Normal::SUC){
            return $response;
        }
        if(empty($response['data']['faces'])){
            return Normal::returnCode(Normal::FAIL,'未检测出人脸头像,请重新识别',$response);
        }
        if($response['data']['face_num']!=1){
            return Normal::returnCode(Normal::FAIL,'人像识别包含多人头像,请重新识别',$response);
        }
        $face=$response['data']['faces'][0];
        if($face['attributes']['facequality']['value']<=$face['attributes']['facequality']['threshold']){
            return Normal::returnCode(Normal::FAIL,'人像识别图片质量欠佳,请重新识别',$response);
        }
        $data=[
            'face_token'=>$face['face_token'],
            'facequality'=>$face['attributes']['facequality'],
            'beauty'=>$face['attributes']['beauty']
        ];
        Face::instance()->add([
            'cd'=>time(),
            'facetoken'=>$face['face_token'],
            'image_base64'=>$param['image_base64'],
            'mn_sid'=>$param['mn_sid']??0,
            'user_id'=>$param['user_id']??0
        ]);
        return   Normal::returnCode(Normal::SUC, '识别成功',$data);
    }
    
    public function messageDeal(array $param) {
        $user= User::instance()->info(['account'=>$param['account']]);
        if(empty($user)){
            return   Normal::returnCode(Normal::FAIL, '未找到用户');
        }
        if(!isset($param['actionType'])){
            return   Normal::returnCode(Normal::FAIL, '消息类型未知');
        }
        $actionType=$param['actionType'];
        if(!in_array($actionType, ['add','edit'])){
           return   Normal::returnCode(Normal::FAIL, '消息类型错误'); 
        }
        if($actionType=='add'){
            if(!isset($param['cid'])||!isset($param['mn_sid'])||!isset($param['face_token'])||!isset($param['image_base64'])){
                return   Normal::returnCode(Normal::FAIL, '参数错误'); 
            }
            $addData=[
                'cd'=>time(),
                'cid'=>$param['cid'],
                'mn_sid'=>$param['mn_sid'],
                'face_token'=>$param['face_token'],
                'image_base64'=>$param['image_base64'],
                'user_id'=>$user['id']??0
            ];
            $response= Face::instance()->add($addData);
        }
        if($actionType=='edit'){
            if(!isset($param['cid'])||!isset($param['mn_sid'])||!isset($param['face_token'])){
                return   Normal::returnCode(Normal::FAIL, '参数错误'); 
            }
            $face= Face::instance()->info(['face_token'=>$param['face_token']]);
            if(empty($face)){
                return   Normal::returnCode(Normal::FAIL, '未找到相关数据'); 
            }
            $editData=[
                'md'=>time()
            ];
            if(isset($param['idcard'])){
                $editData['idcard']=$param['idcard'];
            }
            if(isset($param['name'])){
                $editData['name']=$param['name'];
            }
            if(isset($param['mobile'])){
                $editData['mobile']=$param['mobile'];
            }
            $response= Face::instance()->edit(['id'=>$face['id']],$editData);
        }
        if($response){
            return   Normal::returnCode(Normal::SUC, '操作成功'); 
        }else{
            return   Normal::returnCode(Normal::FAIL, '操作失败'); 
        }
    }
}
